package com.xiyu.service.service.material;

import com.xiyu.service.vo.material.supplier.SupplierSimpleListOutput;
import com.xiyu.service.vo.material.supplier.SupplierSingleGetOutput;
import com.xiyu.service.vo.material.supplier.SupplierUpdateInput;
import com.xiyu.service.vo.material.supplier.SupplierCreateInput;
import com.xiyu.service.model.material.info.MaterialSupplier;
import com.xiyu.service.repository.material.info.MaterialSupplierRepository;
import com.xiyu.service.vo.material.supplier.SupplierPageQueryOutput;
import com.xiyu.service.vo.material.supplier.SupplierPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.material.SupplierConvert;
import com.xiyu.service.util.entity.EntityUtils;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.material.SupplierErrorCode.*;

/**
 * 供应商管理 Service 实现类
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private MaterialSupplierRepository materialSupplierRepository;


    @Override
    public PageResult<SupplierPageQueryOutput> pageQuery(SupplierPageQueryInput inputVO) {
        Page<MaterialSupplier> pageMaterialSupplier = materialSupplierRepository.pageQuery(inputVO);
        List<SupplierPageQueryOutput> listMaterialSupplier = SupplierConvert.INSTANCE.pageQueryOutputConvert(pageMaterialSupplier.getRows());
        return new PageResult<>(listMaterialSupplier, pageMaterialSupplier.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(SupplierCreateInput inputVO) {
        Optional<MaterialSupplier> optionalDuplicateMaterialSupplier = materialSupplierRepository.findFirstBySupplierName(inputVO.getSupplierName());
        if(optionalDuplicateMaterialSupplier.isPresent()){
            throw exception(MATERIAL_SUPPLIER_NAME_DUPLICATE);
        }
        MaterialSupplier newMaterialSupplier = SupplierConvert.INSTANCE.createInputConvert(inputVO);
        newMaterialSupplier = materialSupplierRepository.insert(newMaterialSupplier);
        return newMaterialSupplier.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SupplierUpdateInput inputVO) {
        Optional<MaterialSupplier> optionalDuplicateMaterialSupplier = materialSupplierRepository.findFirstBySupplierName(inputVO.getSupplierName());
        if(optionalDuplicateMaterialSupplier.isPresent() && !inputVO.getId().equals(optionalDuplicateMaterialSupplier.get().id())){
            throw exception(MATERIAL_SUPPLIER_NAME_DUPLICATE);
        }
        Optional<MaterialSupplier> optionalOldMaterialSupplier = materialSupplierRepository.findById(inputVO.getId());
        if(!optionalOldMaterialSupplier.isPresent()){
            throw exception(MATERIAL_SUPPLIER_NOT_EXIST);
        }
        MaterialSupplier updateMaterialSupplier = SupplierConvert.INSTANCE.updateInputConvert(inputVO);
        materialSupplierRepository.update(updateMaterialSupplier);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<MaterialSupplier> optionalDeleteMaterialSupplier = materialSupplierRepository.findById(id);
        if(!optionalDeleteMaterialSupplier.isPresent()){
            throw exception(MATERIAL_SUPPLIER_NOT_EXIST);
        }
        materialSupplierRepository.deleteById(id);
        return true;
    }

    @Override
    public SupplierSingleGetOutput singleGet(Long id) {
        Optional<MaterialSupplier> optionalMaterialSupplier = materialSupplierRepository.findById(id);
        if(!optionalMaterialSupplier.isPresent()){
            throw exception(MATERIAL_SUPPLIER_NOT_EXIST);
        }
        return SupplierConvert.INSTANCE.singleGetOutputConvert(optionalMaterialSupplier.get());
    }

    @Override
    public List<SupplierSimpleListOutput> simpleList() {
        List<MaterialSupplier> materialSupplierList = materialSupplierRepository.findAll();
        return SupplierConvert.INSTANCE.simpleListOutputConvert(materialSupplierList);
    }

}
